// echo partial file, should not compile along

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
using Utils;
namespace GongfaEffect;

public partial class GongfaEffect {
    public static class TianCan {
        public const short Defkey_TianCan=294; // 目前这个方法并没有被暴露在defkey之外，因此只能如此使用。
        // 当然，这里直接patch整个genderkeepyoung了，所以洗髓经也适用这个patch
        public static void Patch(RobustHarmonyInstance HarmonyInstance, string ModIdStr){Utils.GetOffset();
            HarmonyInstance.PatchAll(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType);
            typeof(Config.MedicineItem).GetField("Duration").SetValue(med,(short)32000);
            typeof(Config.MedicineItem).GetField("WugGrowthType").SetValue(med,(sbyte)4);
        }
        static Config.MedicineItem med=new Config.MedicineItem();
        static MethodInfo OfflineTryReplaceWug=typeof(GameData.Domains.Character.Character).GetMethod("OfflineTryReplaceWug",(BindingFlags)(-1));
        static FieldInfo TemplateId=typeof(Config.MedicineItem).GetField("TemplateId"); // short
        [HarmonyPostfix]
        [HarmonyPatch(typeof(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.TianCanShiGu), "OnEnable")]
        public static void OnEnable(GameData.Domains.SpecialEffect.CombatSkill.Wuxianjiao.DefenseAndAssist.TianCanShiGu __instance, GameData.Common.DataContext context){
            if(GameData.Domains.DomainManager.Character.TryGetElement_Objects(__instance.CharacterId, out var character)){
                // logger($"TianCan.OnEnable called for {__instance.CharacterId}");
                var eatingItems = character.GetEatingItems();
                bool flag=false;
                for(sbyte wugType=0;wugType<8;wugType++){
                    var x=eatingItems.IndexOfWug(wugType);
                    if(x!=-1){
                        flag=true;
                        TemplateId.SetValue(med, GameData.Domains.Item.ItemDomain.GetWugTemplateId(wugType, 1));
                        OfflineTryReplaceWug.Invoke(character, new object[]{context, x, med});
                    }
                }
                if(flag)character.SetEatingItems(ref character.GetEatingItems(), context);
            }
        }
    }
}
